import { GetTestApi } from "apis/index";
import { convertUnit } from "utils/index";
const tempNavs = [
  { type: 1, name: "chart", text: "最近播放", num: 10 },
  { type: 2, name: "richText", text: "我喜欢的", num: 10 },
];
Page({
  data: {
    list: [],
    isIos: false,
  },
  onLoad(options) {
    const { type = 1 } = options;
    this.getList();
    this.getNavs(type);
    this.setData({
      isIos: !!wx.getSystemInfoSync().system.includes("iOS"),
    });
  },
  onShareAppMessage() {},
  getList() {
    GetTestApi(false).then((res) => {
      this.setData({
        list: [],
      });
    });
  },
  togglePlay() {},
  //点击一级分类按钮后，自动滚动进行居中处理
  autoScrollToCenter(e) {
    const { currInd: ind } = this.data;
    wx.createSelectorQuery()
      .selectAll(".navs > .item")
      .boundingClientRect()
      .exec((nodes) => {
        if (nodes[0] && nodes[0][ind]) {
          let { left = 0, width = 0 } = nodes[0][ind];
          if (e) {
            left = e.currentTarget.offsetLeft;
          }
          left = convertUnit(left, "pxToRpx");
          width = convertUnit(width, "pxToRpx");
          const diff = left - 375;
          this.setData({
            scrollLeft: convertUnit(diff + width / 2),
          });
        }
      });
  },
  getNavs(type) {
    setTimeout(() => {
      this.setData({
        navs: tempNavs,
        currName: type,
        currInd: tempNavs.findIndex((it) => it.type === type),
      });
      this.autoScrollToCenter();
    }, 100);
  },
  changeNav(e) {
    const { ind } = e.currentTarget.dataset;
    this.setData({
      currInd: ind,
      currName: tempNavs[ind].name,
    });
    this.autoScrollToCenter(e);
  },
});
